<template>
  <div>
    <van-picker title="更改性别" show-toolbar :columns="columns" @confirm="onConfirm" @cancel="onCancel" />
  </div>
</template>

<script>
import { updateUser } from '@/api/user'
export default {
  name: 'UpdateGender',
  props: {
    gender: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女']
    }
  },
  methods: {
    async onConfirm (value, index) {
      try {
        const { data: res } = await updateUser({
          gender: index
        })
        console.log(res)
        this.$emit('close')
        this.$emit('update')
        this.$toast.success('修改性别成功')
      } catch (err) {
        console.dir(err)
        this.$toast.fail('修改性别失败')
      }
    },
    onCancel () {
      this.$emit('close') // 触发父组件里自定义的close事件
    }
  }
}
</script>

<style scoped lang="less">
</style>
